package com.ouc.dataMange.controller;

import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.ouc.common.config.OUCAjaxResult;
import com.ouc.common.utils.file.FileUtils;
import com.ouc.dataMange.domain.SysUploadFile;
import com.ouc.dataMange.service.ISysUploadFileService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ouc.common.annotation.Log;
import com.ouc.common.core.controller.BaseController;
import com.ouc.common.core.domain.AjaxResult;
import com.ouc.common.enums.BusinessType;
import com.ouc.dataMange.domain.SysDataDownload;
import com.ouc.dataMange.service.ISysDataDownloadService;
import com.ouc.common.utils.poi.ExcelUtil;
import com.ouc.common.core.page.TableDataInfo;

/**
 * dataDownloadController
 *
 * @author yuhao
 * @date 2025-07-17
 */
@RestController
@RequestMapping("/datamange/dataDownload")
public class SysDataDownloadController extends BaseController {
  @Autowired
  private ISysDataDownloadService sysDataDownloadService;

  @Autowired
  private ISysUploadFileService sysUploadFileService;

  /**
   * 查询dataDownload列表
   */
//    @PreAuthorize("@ss.hasPermi('datamange:dataDownload:list')")
  @GetMapping("/list")
  public TableDataInfo list(SysDataDownload sysDataDownload) {
    startPage();
    List<SysDataDownload> list = sysDataDownloadService.selectSysDataDownloadList(sysDataDownload);
    return getDataTable(list);
  }

  /**
   * 导出dataDownload列表
   */
  @PreAuthorize("@ss.hasPermi('datamange:dataDownload:export')")
  @Log(title = "dataDownload", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, SysDataDownload sysDataDownload) {
    List<SysDataDownload> list = sysDataDownloadService.selectSysDataDownloadList(sysDataDownload);
    ExcelUtil<SysDataDownload> util = new ExcelUtil<SysDataDownload>(SysDataDownload.class);
    util.exportExcel(response, list, "dataDownload数据");
  }

  /**
   * 获取dataDownload详细信息
   */
  @PreAuthorize("@ss.hasPermi('datamange:dataDownload:query')")
  @GetMapping(value = "/{id}")
  public AjaxResult getInfo(@PathVariable("id") Integer id) {
    return success(sysDataDownloadService.selectSysDataDownloadById(id));
  }

  /**
   * 新增dataDownload
   */
  @PreAuthorize("@ss.hasPermi('datamange:dataDownload:add')")
  @Log(title = "dataDownload", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@RequestBody SysDataDownload sysDataDownload) {
    return toAjax(sysDataDownloadService.insertSysDataDownload(sysDataDownload));
  }

  /**
   * 修改dataDownload
   */
  @PreAuthorize("@ss.hasPermi('datamange:dataDownload:edit')")
  @Log(title = "dataDownload", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@RequestBody SysDataDownload sysDataDownload) {
    return toAjax(sysDataDownloadService.updateSysDataDownload(sysDataDownload));
  }

  /**
   * 删除dataDownload
   */
  @PreAuthorize("@ss.hasPermi('datamange:dataDownload:remove')")
  @Log(title = "dataDownload", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public AjaxResult remove(@PathVariable Integer[] ids) {
    return toAjax(sysDataDownloadService.deleteSysDataDownloadByIds(ids));
  }

  /**
   * 审核任务
   */
  @ResponseBody
  @RequestMapping("/dataCheck")
  public OUCAjaxResult dataCheck(@RequestParam(value = "dataDownloadId") Integer dataDownloadId,
                                 @RequestParam(value = "checkUserId") Integer checkUserId) {
    SysDataDownload sysDataDownload = new SysDataDownload();
    sysDataDownload.setId(dataDownloadId);
    sysDataDownload.setDataCheck(1);
    sysDataDownload.setDataCheckUser(checkUserId);
    sysDataDownload.setDataDownloadNum(0);
    sysDataDownloadService.updateSysDataDownload(sysDataDownload);
    return OUCAjaxResult.ok("审核完毕");
  }

  /**
   * 文件下载（包含预览）
   */
  @ResponseBody
  @RequestMapping("/dataDownload")
  public OUCAjaxResult dataDownload(HttpServletResponse response, String filePath,Integer fileId) throws IOException {

//    response.addHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(fileName,"UTF-8"));//文件预览
//    String filePath = ROOT_PATH+ File.separator+fileName;//拿到文件的路径
    File file = new File(filePath);
    if (!file.exists()) {//文件不存在，就不用管，因为没有东西可写
      return OUCAjaxResult.error("文件不存在");
    }
    SysUploadFile sysUploadFile = new SysUploadFile();
    sysUploadFile.setFileId(fileId);
    sysUploadFile.setUploadNum(sysUploadFileService.selectSysUploadFileByFileId(fileId).getUploadNum()+1);
    sysUploadFileService.updateSysUploadFile(sysUploadFile);

    response.setCharacterEncoding("utf-8");
    response.setContentType("multipart/form-data");
    FileUtils.writeBytes(filePath, response.getOutputStream());

    return OUCAjaxResult.ok("下载完毕");
  }
}
